package com.lfs.admin.service.impl;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lfs.admin.dao.ShopDao;
import com.lfs.admin.feign.AdapterFeignService;
import com.lfs.admin.model.dto.PlatformDto;
import com.lfs.admin.model.entity.CompanyInfoEntity;
import com.lfs.admin.model.entity.ShopEntity;
import com.lfs.admin.service.PlatformService;
import com.lfs.admin.service.system.TokenService;
import com.lfs.base.dto.Result;
import com.lfs.base.util.StringUtils;
import com.lfs.common.constant.CommonConstants;
import com.lfs.common.core.domain.entity.SysUser;
import com.lfs.common.core.domain.model.LoginUser;
import com.lfs.common.core.redis.RedisCache;
import com.lfs.common.utils.ServletUtils;
import com.lfs.dao.entity.PageBean;
import com.lfs.interfaces.dao.PlatformDao;
import com.lfs.interfaces.dto.TokenCreateDto;
import com.lfs.interfaces.model.PlatformEntity;
import com.lfs.interfaces.model.vo.LogisticsTemplateVo;
import com.lfs.interfaces.model.vo.MallInfoVo;
import com.lfs.interfaces.model.vo.TokenCreateVo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class PlatformServiceImpl implements PlatformService {

    @Autowired
    private PlatformDao platformDao;

    @Autowired
    private AdapterFeignService adapterFeignService;

    @Resource
    private RedisCache redisCache;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ShopDao shopDao;

    @Override
    public List<PlatformEntity> queryPlatFormList(PlatformDto platformDto) {
        return platformDao.queryPlatformList();
    }

    @Override
    public String getAuthUrl(TokenCreateDto tokenCreateDto) {
        String url = null;
        Result<?> result = adapterFeignService.getAuthUrl(tokenCreateDto);
        if(result.getCode() == 200 && result.getMessage().equals("success")){
            url = result.getValue().toString();
        }
        return url;
    }

    @Override
    public TokenCreateVo getAccessToken(TokenCreateDto tokenCreateDto) {
        TokenCreateVo tokenCreateVo = new TokenCreateVo();
        String accessToken = "";
        Result<TokenCreateVo> result =  adapterFeignService.getAccessToken(tokenCreateDto);
        if(null != result && result.getCode() == 200){
            tokenCreateVo = result.getValue();
            accessToken = tokenCreateVo.getAccessToken();
            // 设置该店铺的accessToken有效期
            redisCache.setCacheObject(String.valueOf("shopId:"+tokenCreateDto.getShopId()),accessToken,tokenCreateVo.getExpiresIn(), TimeUnit.MILLISECONDS);
            return tokenCreateVo;
        }
        return tokenCreateVo;
    }

    @Override
    public MallInfoVo getMallInfo(TokenCreateDto tokenCreateDto) {
        Result<MallInfoVo> result = adapterFeignService.getMallInfo(tokenCreateDto);
        return result.getValue();
    }

    @Override
    public JSONObject getGoodsCats() {
        Result<JSONObject> result = adapterFeignService.getGoodsCats();
        return result.getValue();
    }

    @Override
    public List<LogisticsTemplateVo> getLogisticsTemplate() {
        Result<List<LogisticsTemplateVo>> result = adapterFeignService.getLogisticsTemplate();
        return result.getValue();
    }

    @Override
    public JSONObject getGoodsCatRule() {
        Result<JSONObject> result = adapterFeignService.getGoodsCats();
        return result.getValue();
    }

    @Override
    public String releaseProduct() {
        Result<String> result =  adapterFeignService.releaseProduct();
        return result.getValue();
    }

    @Override
    public String getPageCode(JSONObject jsonObject) {
        Result<String> result = adapterFeignService.getPageCode(jsonObject);
        return result.getValue();
    }
}
